package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;



    /**
     * 查询用户所有订单
     * @param request
     * @return
     */
    @Override
    public OrderListResponse queryOrder(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            Example orderExample = new Example(Order.class);
            Example.Criteria criteria = orderExample.createCriteria();
            orderExample.setOrderByClause(request.getSort() + " " + "desc");
            criteria.andEqualTo("userId", request.getUserId());
            List<Order> orders = orderMapper.selectByExample(orderExample);
            PageInfo<Order> pageInfo = new PageInfo<>(orders);
            Long total = pageInfo.getTotal();
            response.setTotal(total);
            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            for (Order order : orders) {
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail1(order, orderItemDtos, orderShippingDto);
                orderDetailInfos.add(orderDetailInfo);
            }
            response.setData(orderDetailInfos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.info(e.getMessage());
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return response;
        }
        return response;
    }

    /**
     * 根据ID查询用户订单
     * 其他组人代码
     * @param orderDetailRequest
     * @return
     */
    @Override
    public OrderSingleDetailResponse orderDetailById(OrderDetailRequest orderDetailRequest) {
        OrderSingleDetailResponse orderDetailResponse = new OrderSingleDetailResponse();

        try {
            String orderId = orderDetailRequest.getOrderId();

           /* // 获取订单总数,垃圾代码
            int total = orderMapper.selectCount(new Order());*/

            // 根据订单号查询订单表
            Order order = orderMapper.selectByPrimaryKey(orderId);

            // 根据订单号查询订单商品
            Example orderItemExample = new Example(OrderItem.class);
            Example.Criteria orderItemExampleCriteria = orderItemExample.createCriteria();
            orderItemExampleCriteria.andEqualTo("orderId", orderId);
            List<OrderItem> orderItemList = orderItemMapper.selectByExample(orderItemExample);
            List<OrderItemDto> orderItemDto = orderConverter.item2dto(orderItemList);

            // 获取用户信息
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);

            orderDetailResponse.setGoodsList(orderItemDto);
            orderDetailResponse.setOrderStatus(order.getStatus());
            orderDetailResponse.setOrderTotal(order.getPayment());
            orderDetailResponse.setTel(orderShipping.getReceiverPhone());
            orderDetailResponse.setUserId(order.getUserId());
            orderDetailResponse.setUserName(orderShipping.getReceiverName());
            orderDetailResponse.setStreetName(orderShipping.getReceiverAddress());

            orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.orderDetail Occur Exception:" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderDetailResponse, e);
        }
        System.out.println(orderDetailResponse);
        return orderDetailResponse;
    }
}
